//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.core.adapter;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.Validate;
import org.jfab.core.model.CGAnnotation;
import org.jfab.core.model.CGType;

/**
 * Provides an adapter for <code>CGAnnotation</code>.
 * <p>
 * The string format is one line with the annotation type and optionally
 * attributes enclosed in parentheses.
 * </p>
 */
public class CGAnnotationAdapter implements Adapter<CGAnnotation>
{
    /** CGType adapter. */
    private final Adapter<CGType> _cgTypeAdapter;

    /**
     * Construct this object with the given parameter.
     * 
     * @param cgTypeAdapter Code generator type adapter. (required)
     */
    public CGAnnotationAdapter(final Adapter<CGType> cgTypeAdapter)
    {
        Validate.notNull(cgTypeAdapter);

        _cgTypeAdapter = cgTypeAdapter;
    }

    @Override
    public String marshal(final CGAnnotation value)
    {
        String answer = null;

        if (value != null)
        {
            final String name = _cgTypeAdapter.marshal(value.getType());

            if (CollectionUtils.isNotEmpty(value.getAttributes()))
            {
                final StringBuilder sb = new StringBuilder();
                final int size = value.getAttributes().size();

                for (int i = 0; i < size; i++)
                {
                    sb.append(value.getAttributes().get(i));
                    if (i < (size - 1))
                    {
                        sb.append(", ");
                    }
                }

                answer = name + "(" + sb.toString() + ")";
            }
            else
            {
                answer = name;
            }
        }

        return answer;
    }

    @Override
    public CGAnnotation unmarshal(final String value)
    {
        CGAnnotation answer = null;

        if (value != null)
        {
            final int index = value.indexOf("(");

            if (index >= 0)
            {
                final String name = value.substring(0, index);
                final String attributesString = value.substring(index + 1,
                        value.length() - 1);
                final String[] attributes = attributesString.split(",");
                final List<String> attributesList = new ArrayList<String>();
                for (int i = 0; i < attributes.length; i++)
                {
                    attributesList.add(attributes[i].trim());
                }

                final CGType cgType = _cgTypeAdapter.unmarshal(name);
                answer = new CGAnnotation();
                answer.setType(cgType);
                answer.getAttributes().addAll(attributesList);
            }
            else
            {
                final CGType cgType = _cgTypeAdapter.unmarshal(value);

                answer = new CGAnnotation();
                answer.setType(cgType);
            }
        }

        return answer;
    }
}
